import urllib2
import sys
import re
import os
import threading
import random

import redis

import pykka
import time
import eventlet

from BeautifulSoup import BeautifulSoup
from concurrent.futures import *

from eventlet.green import urllib2

import logging


def testLogging():
    logging.basicConfig(level=logging.INFO)
    logging.info("test log")

def fff():
    print "fff"

def testEventlet():
    gt = eventlet.spawn(fff)
    gt2 = eventlet.spawn(fff)
    gt.wait()
    gt2.wait()



def ff(name,t):
    time.sleep(t)
    print "run:"+name+" "+str(t)

def testFuture():
    e = ThreadPoolExecutor(max_workers=5)
    fu1= e.submit(ff,"f1",3)
    fu2 =e.submit(ff,"f2",2)

    fu1.result()
    fu2.result()
    e.shutdown()



class MyClass:
    _id = 0
    def id(self):
        return self._id

    def setId(self,id):
        self._id= id

def testClass():
    mc = MyClass()
    mc.setId(12)
    print mc.id()

def testList():
    list = [1,2,3,4]
    for i in list:
        print i
    print list[-1]
    print len(list)
    list.append(12)
    print list[-1]
    print max(list)

def testTuple():
    t = (1,2,3)
    print t[2]

def testDictionary():
    dict={"name":"zzy","age":32}
    print dict["name"]+" "+str(dict["age"])

def testString():
    str = "abc"
    print str.islower()
    str = "a,b,c"
    print str.split(",")


def httpGet():
    req = urllib2.Request('http://www.baidu.com')
    page = urllib2.urlopen(req)
    for line in page:
        sys.stdout.write(line)

def writeFile():
    file = open("/tmp/t","wb")
    file.write("abc")
    file.close()

def readFile():
    file = open("/tmp/t","rb")
    lines = file.readlines()
    for line in lines:
        print (line)

def testLambda():
    l = lambda x:x*x
    print l(2)

    list = map(lambda x:x+1,[1,2,3])
    print list

    list = filter(lambda x:x%2==0,[2,4,5])
    print list

    sum = reduce(lambda x,y:x+y,[1,2,3])
    print sum


def testRe():
    print re.split(",","a,b,c")
    print re.findall("a.c","abc,def,axcadc")
    it = re.finditer("[a,b,c]","decab")
    for m in it:
        print m.group()

def testOs():
    os.system("pwd")
    os.system("uname -a")

def f(name):
    num = random.randint(1,3)
    threading._sleep(num)
    print "f"+name+" "+str(num)

def testThread():
    t1= threading.Thread(target=f,args=("t1",))
    t2= threading.Thread(target=f,args=("t2",))
    t1.start()
    t2.start()
    t1.join()
    t2.join()

def testRandom():
    print random.randint(1,100)


def testRedis():
    r = redis.StrictRedis(host='182.92.99.229', port=6379, db=0)
    r.set('foo', 'bar')
    print r.get('foo')


class MyActor(pykka.ThreadingActor):
    def on_receive(self, message):
        print 'hi'


def testPykka():
    ma = MyActor.start()
    ma.tell({'msg':'hi'})
    ma.stop()

def testBeautifulSoup():
    html="""
        <html>
        <body>
        <div class="icon_col">
        <h1 class="h1user">crifan</h1>
        </div>
        </body>
        </html>
    """
    soup = BeautifulSoup(html)
    print soup.body.div.h1.text

def testTime():
    print 'begin'
    time.sleep(5)
    print 'end'




# testTime()
# testBeautifulSoup()

# testPykka()

# testRedis()

# testRandom()
# testThread()
# testOs()
# testRe()
# httpGet()
# writeFile()
# readFile()
# testList()
# testString()
# testTuple()
# testDictionary()
# testClass()
# testLambda()
# testFuture()
# testEventlet()
testLogging()